package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.ITeacherDao;
import com.school.base.model.Teacher;
import com.school.common.constant.Constant;
import com.school.common.tags.PageBean;

public class TeacherService {
	private static Logger logger = Logger.getLogger(TeacherService.class);
	private ITeacherDao teacherDao;

	public ITeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public Teacher getTeacherById(int id) {
		return teacherDao.getTeacherById(id);
	}

	public List<Teacher> getTeacherList(Teacher teacher, PageBean pageBean) {
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(teacherDao.getTotalCount(teacher));
		return teacherDao.getTeacherList(teacher, pageBean.getOffset(), pageBean.getPageSize());
	}

	private void validateOption(Teacher teacher) throws Exception {
		if (teacher == null) {
			logger.error("The teacher object is null");
			throw new Exception("teacher 对象为空");
		}
		if (teacher.getRealName() == null || teacher.getRealName().isEmpty()) {
			logger.error("The teacher's realName is empty.");
			throw new Exception("真实姓名不能为空");
		}
	}

	public void updateTeacher(Teacher teacher) throws Exception {
		this.validateOption(teacher);
		if (teacher.getPwd() != null && teacher.getPwd().length() > 0) {
			if (teacher.getPwd().length() < Constant.USER_PASSWORD_MIN_LENGTH) {
				logger.error("Inser teacher fail:pwd length less than " + Constant.USER_PASSWORD_MIN_LENGTH);
				throw new Exception("密码不能小于" + Constant.USER_PASSWORD_MIN_LENGTH + "个字符");
			}
		}
		teacherDao.updateTeacher(teacher);
	}

	public void deleteTeacher(int id) {
		teacherDao.deleteTeacher(id);
	}

	public void insertTeacher(Teacher teacher) throws Exception {
		this.validateOption(teacher);
		if (teacher.getLoginName() == null || teacher.getLoginName().isEmpty()) {
			logger.error("The teacher's loginName is empty.");
			throw new Exception("登录名不能为空");
		}
		if (!this.checkLoginName(teacher.getLoginName(), teacher.getId())) {
			logger.error("The teacher's loginName is used.");
			throw new Exception("登录名重复");
		}
		if (teacher.getPwd() == null || teacher.getPwd().isEmpty()) {
			logger.error("The teacher's password is empty.");
			throw new Exception("密码不能为空");
		}
		if (teacher.getPwd().length() < Constant.USER_PASSWORD_MIN_LENGTH) {
			logger.error("Inser teacher fail:pwd length less than " + Constant.USER_PASSWORD_MIN_LENGTH);
			throw new Exception("密码不能小于" + Constant.USER_PASSWORD_MIN_LENGTH + "个字符");
		}
		teacherDao.insertTeacher(teacher);
	}

	public boolean checkLoginName(String loginName, int teacherId) {
		return teacherDao.checkLoginName(loginName, teacherId);
	}

	public List<Teacher> getAll() {
		return teacherDao.getAll();
	}

	public List<Teacher> getTeacherListByName(String teacherName){
		return teacherDao.getTeacherListByName(teacherName);
	}
}
